// Aachen Flursteuerung v 1.1
#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
#include <Udp.h>         // UDP library from: bjoern@cs.stanford.edu 12/30/2008
#define maxLength 50

#define INPUT_LIGHT 0
#define INPUT_TOILET 0

#define OUTPUT_FLUR_R 3
#define OUTPUT_FLUR_G 9
#define OUTPUT_FLUR_B 6

byte mac[] = { 0xde, 0xad, 0xbe, 0xef, 0xfe, 0xed }; //update with your MAC rnd
byte ip[] = { 192, 168, 178, 18 };

unsigned int localPort = 8888;      // local port to listen on

// the next two variables are set when a packet is received
byte remoteIp[4];        // holds received packet's originating IP
unsigned int remotePort; // holds received packet's originating port

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char  replyBuffer[] = "ok";       // a string to send back

char inputValue[3] = "";
String inString = String(maxLength);
int val;
int r = 0;
int g = 0;
int b = 0;
int f = 0;
int k = 0;
int l = 0;
int KloStatus=0;

boolean statusLicht=false;

int Serialout = 0;
char serin;
char colorBuff[4];
Server server(80);

void setRed(int value) {
     if (value == 0){
       digitalWrite(OUTPUT_FLUR_R, LOW);
     } else {
       TCCR2A = _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
       TCCR2B = _BV(CS22);
       OCR2B = (value);
    } 
}

void setup()
{

  pinMode(3, OUTPUT);      // R  Flur
  pinMode(9, OUTPUT);      // G  Flur  
  pinMode(6, OUTPUT);      // B  Flur
  
  pinMode(2, INPUT);       // Input Klo Kontakt
  pinMode(4, OUTPUT);      // R LED-Zeile Klo
  pinMode(5, OUTPUT);      // G LED-Zeile Klo
  pinMode(7, OUTPUT);      // B LED-Zeile Klo
  
  pinMode(8, OUTPUT);      // Tueroeffner
  
  Serial.begin(9600);      // Falsch !! -> Baudrate wird durch Ethernet Code verändert -> 19200
  Ethernet.begin(mac, ip);
  Udp.begin(localPort);
  server.begin();

  TCCR2A = _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
  TCCR2B = _BV(CS22);
  
  OCR2B = 0;  
  digitalWrite(3, LOW);
  analogWrite(9, 0);    
  analogWrite(6, 0); 

  Serial.println("- Flur Steuerung v 1.1 Initialisiert -");

}


void loop()
{ 
  KloStatus = digitalRead(2);
  
  if (KloStatus == 0){
    digitalWrite(4, LOW);            
  }else{
    digitalWrite(4, HIGH);
  }
  
 
  
  
  if ((millis() % 50) == 0) {
    int adctemp = 0;
    adctemp = analogRead(0);
    //rial.print("ADC Flur = ");
    //rial.print(adctemp);
    if (adctemp > 512) {
      
     statusLicht = false;
     
     if (r == 0){
       digitalWrite(3, LOW);
     }else{
       TCCR2A = _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
       TCCR2B = _BV(CS22);
                
       OCR2B = (r);
    }  
    analogWrite(9, (g));    
    analogWrite(6, (b)); 
    
    } else { 
      
      statusLicht = true;
  
      TCCR2A = _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
      TCCR2B = _BV(CS22);
                
      OCR2B = (254);
      analogWrite(9, (254));    
      analogWrite(6, (254));
    }
  }
  
/**
* UDP SERVER
**/
  
  
     // if there's data available, read a packet
  int packetSize = Udp.available(); // note that this includes the UDP header
  if(packetSize)
  {
    packetSize = packetSize - 8;      // subtract the 8 byte header

    // read the packet into packetBufffer and get the senders IP addr and port number
    Udp.readPacket(packetBuffer,UDP_TX_PACKET_MAX_SIZE, remoteIp, remotePort);


    switch (packetBuffer[0]) {
      case 'r':
        for(int i=1;i <= 3;i++) {
          inputValue[i-1] = packetBuffer[i];
        }
        r = atoi(inputValue);
        Udp.sendPacket("ack\n", remoteIp, remotePort);
        break;
        
      case 'g':
        for(int i=1;i <= 3;i++) {
          inputValue[i-1] = packetBuffer[i];
        }
        g = atoi(inputValue);
        Udp.sendPacket("ack\n", remoteIp, remotePort);
        break;
        
      case 'b':
        for(int i=1;i <= 3;i++) {
          inputValue[i-1] = packetBuffer[i];
        }
        b = atoi(inputValue);
        Udp.sendPacket("ack\n", remoteIp, remotePort);
        break;
        
      case 'w':
        for(int i=1;i <= 3;i++) {
          inputValue[i-1] = packetBuffer[i];
        }
        
        r = atoi(inputValue);
        g = atoi(inputValue);
        b = atoi(inputValue);
        
        Udp.sendPacket("ack\n", remoteIp, remotePort);
        break;
        
      default:
        Udp.sendPacket("rst\n", remoteIp, remotePort);
        break;
    }
    
  }

} 
